fuzhiMgr = {}
QMPlugin = fuzhiMgr
fuzhiMgr.mode = 0
function fuzhiMgr.init()
    if boot.platform == 1 then
        fuzhiMgr.paraConfig = {
            ["打造符"] = { 158,483,903,1787,"93BED3-303030","-23|23|93BED3,-23|-31|93BED3,14|-105|A4CFF5,-5|-107|4865CB,-9|-120|79B3ED,-17|-122|4F66C5,-8|-131|495AC0,-25|-122|4E81E7,2|-114|5A6ACD",0,0.9},
            ["裁缝符"] = { 158,483,903,1787,"93BED3-303030","-25|-17|93BED3,0|-40|93BED3,8|-121|A8D8F9,12|-136|A0C9F9,-8|-137|5E6FD3,-19|-151|5978D5,-18|-137|6183D8,-25|-138|5B74CF,-24|-126|6486DB",0,0.9},
            ["淬炼符"] = {158,483,903,1787,"93BED3-303030","3|-9|93BED3,-8|-53|92BDD3,19|-134|B6DFFE,16|-136|829BE5,3|-143|6374D7,-17|-156|5C78D2,-23|-142|5C75D0,-5|-138|77A7ED,0|-150|6E9EEA",0,0.9},
            ["梦幻币"] = {182,471,905,1761,"2DA3E8-303030","25|-3|EEFFFF,22|-14|93E6FB,-9|-18|33AAEE,-8|12|36A4EB,5|12|32A6E8,13|6|98F0FE,12|-8|71E8FF,-4|-13|3AC2FF,-8|-2|3BBDFF",0,0.95},
            ["购买"] = { 157,1381,322,1678,"515CDB-303030","-1|-114|505BDB,-6|101|4E59D7,12|-47|D0EEFF,-18|-47|CFEEFF,-19|-27|CFEEFF,2|14|D0EEFF,7|18|D0EEFF,-5|45|CFEEFF,-20|43|C9E7FB",2,0.95},
            ["制造符"] = { 38,62,922,488,"182971-303030","4|-115|6597DB,13|43|182971,2|44|182971,-12|33|192B73,-13|11|182971,5|10|182971,-12|-29|182971,9|-50|182971,12|-30|182971",0,0.9},
            ["等级下拉框"] = { 43,475,148,829,"5F7DB5-303030","-13|-12|5A7BB4,-13|9|5A7BB4,-10|99|A5DAFA,-17|-182|A2D7F7,15|-41|B2E2FF",0,0.9},
            ["等级下拉框60"] = { 137,479,525,833,"192A72-303030","-1|-126|A6D9FB,-4|137|A3D9FB,22|4|A7DAFC,9|-19|192A72,-5|-18|192A72,-13|-24|192A72,1|-10|192A72,3|-19|A6D9FB,4|-19|A6D9FB",0,0.9},
            ["等级下拉框60选中"] = {61,492,130,803,"192A72-303030","-6|0|A8DBF9,-4|0|A9DBFA,-8|-10|192A72,-20|-9|192A72,-17|0|192A72,-13|68|5B7CB4,-2|80|5F7EB6,-12|93|5C7CB5,-3|-5|AADBFB",0,0.9},
        }
    else
    end
end

function fuzhiMgr.setMode(mode)
    fuzhiMgr.mode = mode
end

local function findIcon()
    local para = fuzhiMgr.paraConfig["梦幻币"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function zhifu()
    local para = fuzhiMgr.paraConfig["制造符"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function dazao()
    local para = fuzhiMgr.paraConfig["打造符"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function darg()
    local para = fuzhiMgr.paraConfig["等级下拉框"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function darg60()
    local para = fuzhiMgr.paraConfig["等级下拉框60"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end


local function darg60sel()
    local para = fuzhiMgr.paraConfig["等级下拉框60选中"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function caifeng()
    local para = fuzhiMgr.paraConfig["裁缝符"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function cuilian()
    local para = fuzhiMgr.paraConfig["淬炼符"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function buy()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = fuzhiMgr.paraConfig["购买"]
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end)
    return rtn1,rtn2,rtn3
end

local function buyNow()
    local para = fuzhiMgr.paraConfig["购买"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function doDrag()
    local dragBtn,x,y = darg()
    if dragBtn then
        game.tap(x, y)
        game.delay(300)
        local drag60Btn,x,y = darg60()
        if drag60Btn then
            game.tap(x, y)
        end
    end
end

function fuzhiMgr.execTask()
    mlog.info("符纸")
    local count = 1
    while true do
        if fuzhiMgr.mode == 0 then
            mlog.info("检查"..tostring(count))
            if count == 1 then
                mlog.info("打造")
                local tie,x,y = dazao()
                local sel = darg60sel()
                if tie then
                    game.tap(x, y)
                    game.delay(300)
                    doDrag()
                end
            elseif count == 2 then
                mlog.info("裁缝")
                local tie,x,y = caifeng()
                if tie then
                    game.tap(x, y)
                    game.delay(300)
                    doDrag()
                end
            else
                mlog.info("淬炼")
                local jingshi,x,y = cuilian()
                if jingshi then
                    game.tap(x, y)
                    game.delay(300)
                    doDrag()
                end
            end

            local icon,x,y = findIcon()
            local sel = darg60sel()
            if icon and sel then
                mlog.info("findIcon")
                game.tap(x, y)
                game.delay(500)
                local buybtn,x,y = buy()
                if buybtn then
                    mlog.info("buybtn")
                    game.tap(x, y)
                    game.delay(200)
                end
            end

            local zhifuBtn,x,y = zhifu()
            if zhifuBtn then
                game.tap(x, y)
                game.delay(200)
            end

            count = count + 1
            if count > 3 then
                count = 1
            end
        else

            local para = fuzhiMgr.paraConfig["等级下拉框60"]
            local itemX1,itemY1 = game.findMultiColor(utils.unpack(para))
            local para = fuzhiMgr.paraConfig["等级下拉框60选中"]
            local itemX2,itemY2 = game.findMultiColor(utils.unpack(para))
            local buyCount = 0
            while true do
                -- doDrag()
                if buyCount > 20 then
                    return
                end
                mlog.info("已买到="..tostring(buyCount))
                game.tap(102,629)
                game.tap(360,652)
                game.tap(780,679)
                game.delay(200)
                game.tap(782,681)
                local buybtn,x,y = buyNow()
                while buybtn do
                    mlog.info("购买")
                    game.tap(x, y)
                    game.delay(200)
                    buybtn,x,y = buyNow()
                    game.delay(200)
                    if not buybtn then
                        buyCount = buyCount + 1
                    end
                end
            end
        end
    end
end